<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="排序" prop="sort">
				<el-input-number v-model="form.sortNo" :min="1"></el-input-number>
			</el-form-item>
			<el-form-item label="文件名称" prop="fileName">
				<el-input v-model="form.fileName" placeholder="文件名称" clearable maxlength="40"></el-input>
			</el-form-item>
			<el-form-item label="文件版本" prop="version">
				<el-input v-model="form.version" placeholder="文件版本" clearable maxlength="40"></el-input>
			</el-form-item>
			<el-form-item label="关联文件" prop="attachId">
				<sc-upload-file :apiObj="apiObj" v-model="fileList" :limit="1" :data="{module:'fileMap'}"
								:showFileList="false" @success="addNewFile"
								tip="只能上传单个文件不要超过10M,请上传Word格式文件"  accept="application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
			<el-form-item label="附件权限" prop="attachRole">
				<el-radio-group v-model="form.attachRole">
					<el-radio-button label="1">查看</el-radio-button>
					<el-radio-button label="2">下载</el-radio-button>
					<el-radio-button label="3">打印</el-radio-button>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="form.remark" maxlength="300" clearable type="textarea"></el-input>
			</el-form-item>
			<msg-box-check-box v-model:msgType="form.msgType" code="file-map"></msg-box-check-box>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
import AttachMenu from '@/views/module/attachMenu'
import msgBoxCheckBox from "@/views/module/msgBoxCheckBox";
export default {
	components: {
		AttachMenu,
		msgBoxCheckBox
	},
	emits: ['success', 'closed'],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: '新增',
				edit: '编辑',
				show: '查看'
			},
			apiObj: this.$API.file.attach.uploadFile,
			visible: false,
			isSaveing: false,
			flowOptions: [],
			stepOptions: [],
			fileList: [],
			//表单数据
			form: {
				itemId: '',
				recordId: '',
				sortNo: 1,
				msgType:'',
				fileName: "",
				version: "",
				attachId: "",
				attachRole:"3",
				remark: ""
			},
			//验证规则
			rules: {
				fileName: [
					{required: true, message: '文件名称不能为空'}
				],
				version: [
					{required: true, message: '文件不能为空'}
				],
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		//表单提交方法
		submit() {
			if (this.mode == "add") {
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res = await this.$API.file.fileMap.insertFileMapRecord.post(this.form);
						this.isSaveing = false;
						if (res.code == 200) {
							this.$emit('success')
							this.visible = false;
							this.$message.success("操作成功")
						} else {
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			} else if (this.mode == "edit") {
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res = await this.$API.file.fileMap.updateFileMapRecord.post(this.form);
						this.isSaveing = false;
						if (res.code == 200) {
							this.visible = false;
							this.$emit('success')
							this.$message.success("操作成功")
						} else {
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			}
		},
		addNewFile(res, file)
		{
			this.form.attachId = res.data.attachId;
		},
		setData(data) {
			if(this.mode=="add")
			{
				this.form.itemId = data.itemId
			}else
			{
				this.form.recordId = data.recordId
				this.form.itemId = data.itemId
				this.form.sortNo = data.sortNo
				this.form.fileName = data.fileName
				this.form.version = data.version
				this.form.attachId = data.attachId
				this.form.attachRole = data.attachRole
				this.form.remark = data.remark
			}
		},
	}
}
</script>

<style>
</style>
